##################################################################################################################################
#
# Copyright (C) 2008-2013 Mind System Connection. Lda. Part of Daidze Software, created by Siqsuruq. All other rights reserved.
#
# Mind System Connection Webpage: www.msysc.org
# Daidze Software Page: www.daidze.com www.daidze.org
# E-mail: admin@msysc.org, siqsuruq@gmail.com
#
##################################################################################################################################

proc total_sales_by_client {} {
	$::daidze_main load_main report total_sales_by_client.xml $::main
	$::daidze_main update_form [dict create total_sales [$::daidze_main sum_tablelist_column total_sales_by_client 1]]
}

proc update_total_sales_by_client {} {
	$::daidze_main update_form [dict create total_sales [$::daidze_main sum_tablelist_column total_sales_by_client 1]]
}

proc total_sales_by_client_detail {w} {
	set cli_id [$::db select_id_by_name client [$w get]]
	set columns [list id custom_number inv_date total]
	set result [$::db select_all invoice $columns "fk_client=\'$cli_id\'"]
	$::daidze_main update_form [dict create total_sales_by_client $result]
	$::daidze_main update_form [dict create total_sales [$::daidze_main sum_tablelist_column total_sales_by_client 3]]
}

proc total_debt_by_client {} {
	$::daidze_main load_main report client_debt.xml $::main
	$::daidze_main update_form [dict create total_debt [$::daidze_main sum_tablelist_column total_debt_by_client 1]]
}

proc update_total_debt_by_client {} {
	$::daidze_main update_form [dict create total_debt [$::daidze_main sum_tablelist_column total_debt_by_client 1]]
}


proc update_general_sales {} {
	$::daidze_main update_form [dict create total_with_tax [round [$::daidze_main sum_tablelist_column gen_sales 8]]]
	$::daidze_main update_form [dict create total_without_tax [round [$::daidze_main sum_tablelist_column gen_sales 9]]]
	$::daidze_main update_form [dict create total_tax [round [$::daidze_main sum_tablelist_column gen_sales 10]]]
}



proc product_sales_by_date {} {
	$::daidze_main load_main report product_by_date.xml $::main
	::report::update_product_sales_by_date
}

proc update_product_sales_by_date {} {
	$::daidze_main update_form [dict create price [round [$::daidze_main sum_tablelist_column product_by_date 8]]]
	$::daidze_main update_form [dict create derived_tax [round [$::daidze_main sum_tablelist_column product_by_date 9]]]
	$::daidze_main update_form [dict create derived_discount [round [$::daidze_main sum_tablelist_column product_by_date 10]]]
	$::daidze_main update_form [dict create derived_line_total [round [$::daidze_main sum_tablelist_column product_by_date 11]]]
}

proc general_sales {} {
	$::daidze_main load_main report general_sales.xml $::main
	::report::update_general_sales
}

proc client_debt_detail {w} {
	set result [$::db select_all invoice_vs_aggregated_payment * "debt > 0 AND client_name = \'[$w get]\'"]
	$::daidze_main update_form [dict create debt_by_client $result]
	$::daidze_main update_form [dict create total_debt [round [$::daidze_main sum_tablelist_column debt_by_client 8]]]	
}


proc product_negative_qty_on_stock {} {
set res [$::db select_all on_stock {warehouse_name product_name stock_now} {stock_now <= 0}]
	return $res
}

proc by_product_details {} {
	$::daidze_main load_main report sales_by_product_detail.xml $::main
	$::daidze_main update_form [dict create price_sum [round [$::daidze_main sum_tablelist_column total_sales_by_product_detail 7]]]
	$::daidze_main update_form [dict create tax_sum [round [$::daidze_main sum_tablelist_column total_sales_by_product_detail 8]]]
	$::daidze_main update_form [dict create discount_sum [round [$::daidze_main sum_tablelist_column total_sales_by_product_detail 9]]]
	$::daidze_main update_form [dict create total_sum [round [$::daidze_main sum_tablelist_column total_sales_by_product_detail 10]]]
}

proc update_sums_by_product_details {} {
	$::daidze_main update_form [dict create price_sum [round [$::daidze_main sum_tablelist_column total_sales_by_product_detail 7]]]
	$::daidze_main update_form [dict create tax_sum [round [$::daidze_main sum_tablelist_column total_sales_by_product_detail 8]]]
	$::daidze_main update_form [dict create discount_sum [round [$::daidze_main sum_tablelist_column total_sales_by_product_detail 9]]]
	$::daidze_main update_form [dict create total_sum [round [$::daidze_main sum_tablelist_column total_sales_by_product_detail 10]]]
}